package com.yty.service.impl;

import com.yty.dao.DeptMapper;
import com.yty.pojo.Dept;
import com.yty.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> findAll() {
        List<Dept> depts = deptMapper.findAll();
        return depts;
    }

    @Override
    public int deleteDeptById(Integer id) {

        return deptMapper.deleteDeptById(id);
    }

    @Override
    public int addDept(Dept dept) {
        if (dept.getName() != null && !dept.getName().equals("")){
            dept.setCreateTime(LocalDateTime.now());
            dept.setUpdateTime(LocalDateTime.now());
            return deptMapper.addDept(dept);
        }
        return 0;
    }

    @Override
    public Dept findDeptById(Integer id) {
        if (id != null && id > 0){
           return deptMapper.findDeptById(id);
        }else return null;
    }

    @Override
    public int updateDept(Dept dept) {
        if (dept.getId() != null && dept.getId() > 0 && dept.getName() != null && !dept.getName().equals("")){
            dept.setUpdateTime(LocalDateTime.now());
            return deptMapper.updateDept(dept);
        }
        return 0;
    }
}
